import chen.ImgUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;

@WebServlet("/uploadServlet")
@MultipartConfig(location = "C:\\Users\\ct\\IdeaProjects\\myExperiment3\\src\\main\\webapp\\img")
public class uploadServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        Part part = request.getPart("file");
        //part.getSubmittedFileName()方法获取上传文件的文件名
        String fileName = part.getSubmittedFileName();
        //String fileName = request.getParameter("file");
        System.out.println("上传文件的文件名: " + fileName);
        if(fileName==null || fileName.equals(""))
            response.sendRedirect("/error.html");
        part.write(fileName);
        ImgUpload.upload(fileName,request,response);
        response.sendRedirect("/index.jsp");
    }
}



